package com.pushway.demo.springboot;

import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Prometheus服务类
 *
 * @author chentiefeng
 * @date 2021/4/19 15:14
 */
@ConditionalOnClass(CollectorRegistry.class)
@Service
public class PrometheusService {

    @Autowired
    public PrometheusService(ApplicationContext applicationContext, CollectorRegistry collectorRegistry) {
        Map<String, ? extends Collector> beanMap = applicationContext.getBeansOfType(Collector.class);
        beanMap.entrySet().stream().forEach(tmpEntry -> {
            collectorRegistry.register(tmpEntry.getValue());
        });
    }
}
